package com.lovo.ssc.controller;

import com.lovo.dto.RegisterDto;
import com.lovo.ssc.bean.SysUser;
import com.lovo.ssc.service.ISysUserService;
import com.lovo.util.MQUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Create with IntelliJ IDEA
 * user:zhang
 */
@RestController
@RequestMapping("register")
@Api(tags = "用户注册控制层张小龙")
public class RegisterController {
    @Autowired
    ISysUserService service;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @ApiOperation(value = "快速注册")
    @PostMapping("fastRegister")
    public String fastRegister(SysUser user){
        service.save(user);
        return "ok";
    }


    @Transactional
    @ApiOperation(value = "补齐资料注册")
    @PostMapping("fillMaterials/{qualition}/{idCard}/{userPompany}")
    public String fillMaterials(@PathVariable("qualition")String qualition, @PathVariable("idCard")String idCard, @PathVariable("userPompany")String userPompany, HttpServletRequest request){
        String token=request.getHeader("token");
        SysUser user=service.findByUserName(token);
        user.setUserQualification(qualition);
        user.setUserCard(idCard);
        user.setUserPompany(userPompany);
        service.save(user);

        RegisterDto registerDto=new RegisterDto();
        registerDto.setUserName(user.getUserName());
        registerDto.setUserPwd(user.getUserPwd());
        registerDto.setRealName(user.getRealName());
        registerDto.setUserSex(user.getUserSex());
        registerDto.setUserPhone(user.getUserPhone());
        registerDto.setUserQualification(user.getUserQualification());
        registerDto.setUserCard(user.getUserCard());
        registerDto.setUserPompany(user.getUserPompany());

        rabbitTemplate.convertAndSend(MQUtil.registerExchange, MQUtil.maintainKey,registerDto);

        return "ok";
    }

    @Transactional
    @ApiOperation(value = "普通注册")
    @PostMapping("ordinaryRegister")
    public String ordinaryRegister(SysUser user){
        service.save(user);
        RegisterDto registerDto=new RegisterDto();
        registerDto.setUserName(user.getUserName());
        registerDto.setUserPwd(user.getUserPwd());
        registerDto.setRealName(user.getRealName());
        registerDto.setUserSex(user.getUserSex());
        registerDto.setUserPhone(user.getUserPhone());
        registerDto.setUserQualification(user.getUserQualification());
        registerDto.setUserCard(user.getUserCard());
        registerDto.setUserPompany(user.getUserPompany());
        rabbitTemplate.convertAndSend(MQUtil.registerExchange, MQUtil.maintainKey,registerDto);
        return "ok";
    }

    @GetMapping("test")
    public String test(){

        rabbitTemplate.convertAndSend(MQUtil.registerExchange, MQUtil.maintainKey,"");
        return "ok";
    }
}
